package org.agilelang.editor.view.tree;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Vector;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.agilelang.editor.ViewFrame;

/**
 *
 * @author bk4cybertel
 */
public class ViewTree extends JTree {
    /*
     * Action MENU : NewProject,OpenProject,NewFile TAB : OpenFile, CloseFile
     * TREE : NewFile, DeleteFile, RenameFile
     */

    ViewFrame frame;
    DefaultTreeModel model;

    public ViewTree(ViewFrame frame) {
        this.frame = frame;
        this.setCellRenderer(new RendererTree(this.frame));
    }

    public void setPathProject(String path, String NameProject) {
        model = new DefaultTreeModel(addNodes(null, new File(path)));
        this.setModel(model);
        popup();
    }

    public void reload() {
        model = new DefaultTreeModel(addNodes(null, new File(this.frame.getPathProject())));//+File.separator+"src"
        this.setModel(model);
        popup();
    }

    public void popup() {
        JPopupMenu popup = new JPopupMenu();
        popup.setInvoker(this);
        PopupTree handler = new PopupTree(this, popup);
        this.add(popup);
        //expand(new TreePath(tree.getModel().getRoot()));
    }

    public DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
        String curPath = dir.getPath();
        String namedir = dir.getName();
        DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);//las carpetas
        if (curTop != null) { // should only be null at root
            curTop.add(curDir);
        }
        Vector ol = new Vector();
        String[] tmp = dir.list();

        for (int i = 0; i < tmp.length; i++) {
            ol.addElement(tmp[i]);
        }

        Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
        File f;
        Vector files = new Vector();
        // Make two passes, one for Dirs and one for Files. This is #1.
        for (int i = 0; i < ol.size(); i++) {
            String thisObject = (String) ol.elementAt(i);
            String newPath;
            if (curPath.equals(".")) {
                newPath = thisObject;
            } else {
                newPath = curPath + File.separator + thisObject;
            }
            if ((f = new File(newPath)).isDirectory()) {
                addNodes(curDir, f);
            } else {
                if (newPath.endsWith(".agile") || newPath.endsWith(".xml") || newPath.endsWith(".sql")) {
                    files.addElement(newPath);//los ficheros
                }
            }

        }
        // Pass two: for files.
        for (int fnum = 0; fnum < files.size(); fnum++) {
            curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
        }
        return curDir;
    }

    void rename_to_tab(String toString) {
    }

    void rename_to_tab(String name_old, String name_new) {
        frame.rename_tab(name_old, name_new);
    }

    public void empty() {
        model = null;
        this.setModel(model);
    }

    public void newProject() {

        savetxt(frame.getPathProject() + File.separator + "source" + File.separator + "source.agile", "");
        savetxt(frame.getPathProject() + File.separator + "config" + File.separator + "app.config", "");
        savetxt(frame.getPathProject() + File.separator + "tmpl" + File.separator + "app.tpl", "");
        reload();
    }

    public void newFile(String name, String fileType) {

        /*if(!name.equals(".agile")){
         name += ".agile";
         }*/
        if (fileType.equals("Fichero Fuente")) {
            savetxt(frame.getPathProject() + File.separator + "source" + File.separator + name + ".agile", "");
        }
        if (fileType.equals("Fichero Configuracion")) {
            savetxt(frame.getPathProject() + File.separator + "config" + File.separator + name + ".config", "");
        }
        if (fileType.equals("Ficheros de Templates")) {
            savetxt(frame.getPathProject() + File.separator + "tmpl" + File.separator + name + ".tmpl", "");
        }

        reload();
    }

    public void savetxt(String path, String editor) {
        try {
            FileWriter fw = new FileWriter(path);
            fw.write(editor);
            fw.close();
        } catch (IOException e) {
            System.out.println("Can't open file '' for writing :: " + path);
            return;
        }
    }

    public void openProject() {
        reload();
    }

    public void closeProject() {
        empty();
    }

    /* public void newFile(String generateagile, String file) {
     savetxt(frame.getPathProject() + File.separator + "src" + File.separator + generateagile, file);
        
     reload();
     }*/
}
